//
//  HMHeaderView.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SVProgressHUD

let StatusCellMargin:CGFloat = 8
class HMHeaderView: UIView {
    var topConstraint: Constraint?
    var statusViewModel: HMStatusViewModel?{
        didSet{
            iconView.sd_setImageWithURL(statusViewModel?.url_iconView)
            
            verifiedView.image = statusViewModel?.verified_image
            nameLabel.text = statusViewModel?.name
            mrankView.image = statusViewModel?.mrank_image
            createdLabel.text = statusViewModel?.created_atText
            sourceLabel.text = statusViewModel?.sourceText
            textLabel.attributedText = statusViewModel?.originalAttributeString
            let pictures = statusViewModel?.pic_urls
            pictureView.pictures = pictures
            pictureView.pictures = pictures
            topConstraint?.uninstall()
            if pictures?.count > 0 {
                self.snp_updateConstraints(closure: { (make) in
                    self.topConstraint = (make.bottom.equalTo(pictureView).offset(StatusCellMargin)).constraint
                    pictureView.hidden = false
                })
            }else{
                self.snp_makeConstraints(closure: { (make) in
                    self.topConstraint = (make.bottom.equalTo(textLabel).offset(StatusCellMargin)).constraint
                    pictureView.hidden = true
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.purpleColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        addSubview(iconView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(mrankView)
        addSubview(createdLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(pictureView)
        
        iconView.snp_makeConstraints { (make) in
            make.top.left.equalTo(StatusCellMargin)
            make.height.width.equalTo(40)
        }
        verifiedView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(iconView)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.top.equalTo(StatusCellMargin)
        }
        mrankView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(StatusCellMargin)
        }
        createdLabel.snp_makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(createdLabel.snp_right).offset(StatusCellMargin)
                make.top.equalTo(createdLabel)
        }
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(StatusCellMargin)
            make.right.equalTo(-StatusCellMargin)
            
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLabel)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) in
          self.topConstraint = (make.bottom.equalTo(pictureView).offset(StatusCellMargin)).constraint
        }
        textLabel.highlightTapAction = { (containerView: UIView, text: NSAttributedString, range:  NSRange,rect: CGRect ) -> Void in
            let str = text.string
            let subStr = (str as NSString).substringWithRange(range)
            SVProgressHUD.showInfoWithStatus(subStr)
            if(subStr.containsString("http://")){
                let webVC = HMWebViewController()
                webVC.urlStr = subStr
                self.findNavigationController()?.pushViewController(webVC, animated: true)
            }
        }
    }
    
    
    private var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    private var nameLabel: UILabel = UILabel(title: "小朱", font: UIFont.systemFontOfSize(14), textColor: UIColor.orangeColor(), align:.Center)
    private var mrankView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    private var createdLabel: UILabel = UILabel(title: "11:00", font: UIFont.systemFontOfSize(10), textColor: UIColor.darkGrayColor(), align:.Center)
    private var sourceLabel: UILabel = UILabel(title: "来自小光", font: UIFont.systemFontOfSize(10), textColor: UIColor.darkGrayColor(), align:.Center)
//    private var textLabel: UILabel = UILabel(title: "一道道谁来,一道道玩", font: UIFont.systemFontOfSize(14), textColor: UIColor.orangeColor(), align:.Left)
    private var textLabel: YYLabel = {
        let yyl = YYLabel()
        yyl.numberOfLines = 0
        yyl.font = originalContentLabelFont
        yyl.preferredMaxLayoutWidth = ScreenWidth - 2 * StatusCellMargin
        return yyl
    }()
    private lazy var pictureView: HMPictureView = HMPictureView()
    
}
